//
//  LXFoodEditViewController.m
//  bgms_d
//
//  Created by Mountain on 9/3/2019.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXFoodEditViewController.h"
#import "LXFoodViewController.h"
#import "LXDevSettingModel.h"

@interface LXFoodEditViewController ()
{
    BOOL rangePlace;
}
@end

@implementation LXFoodEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buildNavi];
    
    if (self.editItem.rangeTemp) {
        self.highLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:self.editItem.setNum];
        self.highTempSteper.value = [[[LXDevSettingModel shareSettingModel] tempCTempF:self.editItem.setNum] integerValue];
        self.highTempSteper.maximumValue = [[[LXDevSettingModel shareSettingModel] tempCTempF:@"300"] integerValue];
        self.highTempSteper.minimumValue = [[[LXDevSettingModel shareSettingModel] tempCTempF:@"0"] integerValue];
        self.lowLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:self.editItem.minNum];
        self.lowTempSteper.value = [[[LXDevSettingModel shareSettingModel] tempCTempF:self.editItem.minNum] integerValue];
        self.lowTempSteper.maximumValue = [[[LXDevSettingModel shareSettingModel] tempCTempF:@"300"] integerValue];
        self.lowTempSteper.minimumValue = [[[LXDevSettingModel shareSettingModel] tempCTempF:@"0"] integerValue];
        rangePlace = YES;
    } else {
        self.highLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:self.editItem.setNum];
        self.highTempSteper.value = [[[LXDevSettingModel shareSettingModel] tempCTempF:self.editItem.setNum] integerValue];
        self.heightLayout.constant = 105;
        self.lowTempSteper.hidden = YES;
        self.lowLabel.hidden = YES;
        self.lowNameLabel.hidden = YES;
        rangePlace = NO;
    }
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDown:)];
    gesture.numberOfTapsRequired = YES;
    gesture.numberOfTouchesRequired = YES;
    self.testLineLabel.userInteractionEnabled = YES;
    [self.testLineLabel addGestureRecognizer:gesture];
    
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.testLineLabel.text attributes:attribtDic];
    self.testLineLabel.attributedText = attribtStr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)buildNavi
{
    [self.navigationView drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(-0.5, 0, 0, 0)];
    if (IPHONE_X) {
        self.naviHeight.constant = 66 + 22;
    }
//    UIButton *editBtn = [[UIButton alloc] init];
//    [editBtn setImage:[UIImage imageNamed:@"okIcon"] forState:UIControlStateNormal];
//    [editBtn addTarget:self action:@selector(okDown:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *editBar = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
//    self.navigationItem.rightBarButtonItem = editBar;
}

- (void)tapDown:(UIGestureRecognizer *)gesture {
    if (self.editItem.rangeTemp) {
        self.highLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:self.originItem.setNum];
        self.highTempSteper.value = [[[LXDevSettingModel shareSettingModel] tempCTempF:self.originItem.setNum] integerValue];
        self.lowLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:self.originItem.minNum];
    } else {
        self.highLabel.text = [[LXDevSettingModel shareSettingModel] tempCTempF:self.originItem.setNum];
        self.highTempSteper.value = [[[LXDevSettingModel shareSettingModel] tempCTempF:self.originItem.setNum] integerValue];
        self.heightLayout.constant = 105;
        self.lowTempSteper.hidden = YES;
        self.lowLabel.hidden = YES;
        self.lowNameLabel.hidden = YES;
    }
}

- (IBAction)okDown:(UIButton *)sender
{
    if (rangePlace) {
        if (self.highTempSteper.value <= self.lowTempSteper.value) {
            [EasyTextView showErrorText:NSLocalizedString(@"TheMinTemperatureLess", nil)];
            return;
        }
        self.editItem.setNum = [NSString stringWithFormat:@"%.0f", self.highTempSteper.value];
        self.editItem.minNum = [NSString stringWithFormat:@"%.0f", self.lowTempSteper.value];
    } else {
        self.editItem.setNum = [NSString stringWithFormat:@"%.0f", self.highTempSteper.value];
    }
    
    LXFoodViewController *foodVC =  [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
    foodVC.editItem = self.editItem;
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)backDown:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)highStepValueChange:(UIStepper *)sender {
    self.highLabel.text = [NSString stringWithFormat:@"%.0f%@", sender.value, [LXDevSettingModel shareSettingModel].tempUnitF ? @"°F" : @"°C"];
}

- (IBAction)lowStepValueChange:(UIStepper *)sender {
    self.lowLabel.text = [NSString stringWithFormat:@"%.0f%@", sender.value, [LXDevSettingModel shareSettingModel].tempUnitF ? @"°F" : @"°C"];
}

- (IBAction)removeDown:(UIButton *)sender
{
    if (![self.editItem.image isEqualToString:@"other"]) {
        [EasyTextView showInfoText:NSLocalizedString(@"IsSystemKinds", nil)];
        return;
    }
    LXFoodViewController *foodVC =  [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
    [foodVC removEditItem];
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
